﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ThinningImage
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private MainWindowViewModel viewmodel;

        public MainWindow()
        {
            InitializeComponent();
            DataContext = viewmodel = new MainWindowViewModel();
        }

        public void SaveImage(char character)
        {
            Rect rect = new Rect(canvas.RenderSize);
            RenderTargetBitmap rtb = new RenderTargetBitmap((int)rect.Right, (int)rect.Bottom, 96d, 96d, PixelFormats.Default);
            rtb.Render(canvas);
            //endcode as PNG
            BitmapEncoder pngEncoder = new PngBitmapEncoder();
            pngEncoder.Frames.Add(BitmapFrame.Create(rtb));

            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            pngEncoder.Save(ms);
            ms.Close();
            System.IO.File.WriteAllBytes("data3\\" + character + ".png", ms.ToArray());
        }

        private async void start_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0x4E00; i < 0x9FFF; i++)
            {
                var path = Environment.CurrentDirectory + "\\data\\" + (char)i + ".png";
                viewmodel.OriginalSource = new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute));
                await Task.Delay(1);
                SaveImage((char)i);
            }
        }
    }
}